﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using BCNPCEntities;
using System.Threading;
using bus_TienichNPC;
using Microsoft.Windows.Controls;
using BaocaoNPC.common;
namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for CN_DUYETCHAMNOTNV.xaml
    /// </summary>
    public partial class CN_DUYETCHAMNOTNV : UserControl
    {
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        //public String strMa_TNgan = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap.ToUpper() : "PA";
        //cls_ImportExcel_bus cls = new cls_ImportExcel_bus();
        DataSet dsThuNganVien = new DataSet();
        DataSet dsHdon_Dcham = new DataSet();
        int thang = 0, nam = 0;
        string maloi;
        public CN_DUYETCHAMNOTNV()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManagerWithSelectAll(this);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            dtpNgayNOP.SelectedDate = System.DateTime.Now;
            //cls.select_THANGNAMCONGNO(strMa_dviqly, ref thang, ref nam);
            BaocaoNPC.common.GlobalServices.Service_NPCUS.select_THANGNAMCONGNO(strMa_dviqly, ref thang, ref nam);
            lblthangnam.Content = "Tháng năm công nợ: " + thang.ToString() + "/" + nam.ToString();
            dsThuNganVien = new DataSet();
            //dsThuNganVien = cls.select_ThuNganVien(strMa_dviqly);
            dsThuNganVien = BaocaoNPC.common.GlobalServices.Service_NPCUS.select_ThuNganVien(strMa_dviqly);
            if (dsThuNganVien != null) txtThuNganVien.AddItem(new CMIS2_TextBox.AutoCompleteEntry(dsThuNganVien));
            txtThuNganVien.FocusAutoText();
           
        }

        private void cmdLaydulieu_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dsHdon_Dcham = new DataSet();
                DateTime date;
                //dsHdon_Dcham = cls.select_HOADON_DUYETCHAM(strMa_dviqly, catptu(txtThuNganVien.Text), dtpNgayNOP.SelectedDate.Value.Date);
                if (dtpNgayNOP.Text == string.Empty)
                {
                    date = Convert.ToDateTime("01/01/0001");
                }
                else
                {
                    date = dtpNgayNOP.SelectedDate.Value.Date;
                }
                dsHdon_Dcham = BaocaoNPC.common.GlobalServices.Service_NPCUS.select_HOADON_DUYETCHAM(strMa_dviqly, catptu(txtThuNganVien.Text), date);
                if (dsHdon_Dcham.Tables.Count != 0)
                {
                    dgrHDON_CNO.ItemsSource = dsHdon_Dcham.Tables[0].DefaultView;
                }
            }
            catch
            { }
        }
        private string catptu(string fullname)
        {
            int i = 0;
            i = fullname.IndexOf("_*_");
            string ptu = fullname.Substring(0, i).Trim();
            return ptu;
        }

        private void chkDaoChon_Click(object sender, RoutedEventArgs e)
        {
            Dao_check(dgrHDON_CNO);
            bodem(dgrHDON_CNO);
        }

        private void chkChonAll_Click(object sender, RoutedEventArgs e)
        {
            Check_all(dgrHDON_CNO, chkChonAll);
            bodem(dgrHDON_CNO);
        }
        private void Check_all(DataGrid dg, CheckBox chk)
        {
            DataRowView drw_check = null;
            DataRowView drw = null;
            bool _Check = Convert.ToBoolean(chk.IsChecked);
            if (_Check == true)
            {
                chk.Content = "Bỏ chọn tất cả";
                for (int i = 0; i <= dg.Items.Count - 1; i++)
                {
                    drw = (DataRowView)dg.Items[i];
                    drw["CHON"] = "True";
                }
            }
            else
            {
                chk.Content = "Chọn tất cả";
                for (int i = 0; i <= dg.Items.Count - 1; i++)
                {
                    drw = (DataRowView)dg.Items[i];
                    drw["CHON"] = "False";
                }
            }
        }
        private void Dao_check(DataGrid dg)
        {
            DataRowView drw_check = null;
            DataRowView drw = null;
            for (int i = 0; i <= dg.Items.Count - 1; i++)
            {
                drw = (DataRowView)dg.Items[i];
                if (drw["CHON"].ToString() == "False")
                {
                    drw["CHON"] = "True";
                }
                else
                {
                    drw["CHON"] = "False";
                }
            }
        }
        private void bodem(DataGrid dg)
        {
            try
            {
                DataRowView drw = null;
                int dem = 0;
                Decimal tongTienduyet = 0;
                Decimal tongTienHdon = 0;
                for (int i = 0; i <= dg.Items.Count - 1; i++)
                {
                    drw = (DataRowView)dg.Items[i];
                    if (drw["CHON"].ToString() == "True")
                    {
                        dem++;
                        tongTienduyet = tongTienduyet + Convert.ToDecimal(drw["TTIEN_NO"]);
                    }
                    else
                    {

                    }
                    tongTienHdon = tongTienHdon + Convert.ToDecimal(drw["TTIEN_NO"]);
                }
                if (dem == 0)
                { lbtTongSo.Content = "Hóa đơn duyệt: " + "0/" + dg.Items.Count.ToString(); }
                else
                {
                    lbtTongSo.Content = "Hóa đơn duyệt: " + dem.ToString("# ###") + "/" + dg.Items.Count.ToString();
                }
                txtTienduyet.Text = tongTienduyet.ToString("# ### ### ###");
                txtTongTien.Text = tongTienHdon.ToString("# ### ### ###");
            }
            catch
            { }
        }

        private void CHECK_HOADON_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bodem(dgrHDON_CNO);
            }
            catch
            { }
        }

        private void cmdghi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ERRO = "";
                if (dsHdon_Dcham == null || dsHdon_Dcham.Tables.Count == 0)
                { showErrorMess("Không có dữ liệu chấm nợ"); return; }
                int pcount = dsHdon_Dcham.Tables[0].Select("CHON = True").Count();
                if (pcount == 0)
                { showErrorMess("Không có hóa đơn nào được chọn chấm lần này"); return; }
                if (MessageBox.Show("Bạn có muốn cập nhật chấm nợ cho " + pcount.ToString() + " hóa đơn không?", "Thông báo", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                {
                    //if (cls.chamno_OFFLINE(dsHdon_Dcham, strTenDNHap, thang, nam, ref maloi))
                    if (BaocaoNPC.common.GlobalServices.Service_NPCUS.chamno_OFFLINE_TUNGND(dsHdon_Dcham, strTenDNHap, thang, nam, ref maloi))
                    {
                        showSucrMess("Cập nhật chấm nợ thành công");
                        for (int i = dsHdon_Dcham.Tables[0].Rows.Count - 1; i >= 0; i--)
                        {
                            if (dsHdon_Dcham.Tables[0].Rows[i]["CHON"].ToString() == "True")
                            {
                                dsHdon_Dcham.Tables[0].Rows[i].Delete();
                            }
                        }
                        dgrHDON_CNO.ItemsSource = dsHdon_Dcham.Tables[0].DefaultView;
                        //btnNhanSL.IsEnabled = false;
                    }
                    else
                    {
                        showErrorMess("Lỗi trong quá trình chấm nợ");
                        dgrHDON_CNO.ItemsSource = null;
                    }
                }
            }
            catch (Exception ex)
            { showErrorMess(ex.ToString()); dgrHDON_CNO.ItemsSource = null; }
        }
        public void showErrorMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.ERROR_MESSAGE);
        }
        public void showSucrMess(string s)
        {
            Component.Validate.ValidationManager.showMessOnView(this, 5000, s, Component.Validate.ValidationManager.MESSEGER_TYPE.SUCCESS_MESSAGE);
        }

        private void txtThuNganVien_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                dtpNgayNOP.Focus();
            }
        }
    }
}
